package com.nanbei.eduService.controller;


import com.nanbei.commonUntil.R;
import com.nanbei.eduService.client.UCenterClient;
import com.nanbei.eduService.entity.EduComment;
import com.nanbei.eduService.entity.UcenterMember;
import com.nanbei.eduService.service.EduCommentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-26
 */
@RestController
@RequestMapping("/eduService/eduComment")
@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;

    @Autowired
    private UCenterClient uCenterClient;


    /***
     * 根据课程ID查看课程评论
     * @param courseId
     * @return
     */
    @GetMapping("/findCommentByCourseId/{courseId}/{current}/{limit}")
    public R findCommentByCourseId(@PathVariable String courseId ,
                                    @PathVariable long current,
                                    @PathVariable long limit){
        HashMap commentMap = eduCommentService.findCommentByCourseId(courseId, current, limit);
        String total = (String) commentMap.get("total");
        List<EduComment> eduCommentList = (List<EduComment>) commentMap.get("records");
        return R.ok().data("total",total).data("eduCommentList",eduCommentList);
    }

    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment eduComment, HttpServletRequest request){

        EduComment comment = new EduComment();

        //获取用户信息
        UcenterMember ucenterMember = uCenterClient.userInfoVo(request);

        comment.setMemberId(ucenterMember.getId());
        comment.setNickname(ucenterMember.getNickname());
        comment.setAvatar(ucenterMember.getAvatar());
        comment.setCourseId(eduComment.getCourseId());
        comment.setTeacherId(eduComment.getTeacherId());
        comment.setContent(eduComment.getContent());

        eduCommentService.save(comment);
        return R.ok();
    }

}

